import { EntityPartType } from '@amcax/web-render';
import { ManagerId } from 'nextcae-base';
import { useRenderStore } from '@/common/store';

// 控制体，面，线，点显隐事件
const entityTypeChange = (
  ids: number[],
  isShow,
  databaseName,
  // association?: boolean,
) => {
  const mainRenderer = useRenderStore.getState().mainRenderer;
  const entityPartType = getEntTypeByName(databaseName);
  // 获取GeometryManager
  const geomMgr = mainRenderer.getEntityManager(ManagerId.Geometry);
  // 隐藏点：EntityPartType.point、线：EntityPartType.line、面：EntityPartType.face
  geomMgr.setEntityVisible(isShow, undefined, entityPartType, ids as number[]);
  // 刷新渲染
  mainRenderer.render();
};

const getEntTypeByName = (name) => {
  let entityPartType = null;
  switch (name) {
    case 'FACE':
      entityPartType = EntityPartType.face;
      break;
    case 'LINE':
      entityPartType = EntityPartType.line;
      break;
    case 'POINT':
      entityPartType = EntityPartType.point;
      break;
    case 'SOLID':
      entityPartType = EntityPartType.solid;
      break;
    case 'LINES3D':
      entityPartType = EntityPartType.line;
      break;
    case 'POINTS3D':
      entityPartType = EntityPartType.point;
      break;
    case 'ELEMENT':
      entityPartType = EntityPartType.element;
      break;
  }
  return entityPartType;
};

// 控制选择模型高亮
const entitySelectedTypeChange = (
  selected: boolean | undefined,
  dxid: string | number | undefined,
  dataName: 'FACE' | 'LINE' | 'POINT' | 'SOLID',
  idx: number | number[],
) => {
  const mainRenderer = useRenderStore.getState().mainRenderer;
  let entityPartType = null;
  switch (dataName) {
    case 'FACE':
      entityPartType = EntityPartType.face;
      break;
    case 'LINE':
      entityPartType = EntityPartType.line;
      break;
    case 'POINT':
      entityPartType = EntityPartType.point;
      break;
    case 'SOLID':
      entityPartType = EntityPartType.solid;
      break;
  }

  // 获取GeometryManager
  const geomMgr = mainRenderer.getEntityManager(ManagerId.Geometry);
  // 隐藏点：EntityPartType.point、线：EntityPartType.line、面：EntityPartType.face
  geomMgr.setEntitySelected(selected, dxid, entityPartType, idx);
  // 刷新渲染
  mainRenderer.render();
};
/**
 * 在树形数据中查找指定节点的最顶层父节点和直接父节点
 * @param {Array} treeData 树形数据
 * @param {string} childKey 要查找的子节点key
 * @param {object} parent 当前递归的父节点（内部使用）
 * @param {object} rootParent 最顶层父节点（内部使用）
 * @returns {object|null} 返回包含{rootParent, parent}的对象或null
 */
const findParentNodes = (
  treeData,
  childKey,
  parent = null,
  rootParent = null,
) => {
  for (const node of treeData) {
    if (node.name === childKey) {
      // 返回最顶层父节点和直接父节点
      return {
        rootParent: rootParent,
        parent: parent,
      };
    }
    if (node.children) {
      // 如果是第一层节点，则当前节点成为可能的rootParent
      const currentRootParent = parent === null ? node : rootParent;
      const found = findParentNodes(
        node.children,
        childKey,
        node,
        currentRootParent,
      );
      if (found) return found;
    }
  }
  return null;
};
export {
  entityTypeChange,
  entitySelectedTypeChange,
  getEntTypeByName,
  findParentNodes,
};
